﻿import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { PasswordComplexitySetting, ProfileServiceProxy, UserEditDto, UserServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { StandardPartsFeatureDto } from '@shared/service-proxies/service-proxies';
import { TaskForStandardPartsServiceProxy,TaskForStandardPartsDto,RequirementsForPartsDetailDto } from '@shared/service-proxies/service-proxies';
import * as moment from 'moment';
@Component({
    selector: 'requirementCommissionItemEditComponent',
    templateUrl: './requirement-commission-item_edit.component.html',
    styles: [`.user-edit-dialog-profile-image {
             margin-bottom: 20px;
        }`
    ]
})
export class RequirementCommissionItemEditComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();
    taskForStandardPartsDto: TaskForStandardPartsDto = new TaskForStandardPartsDto();
    requirementsForPartsDetailDto: RequirementsForPartsDetailDto = new RequirementsForPartsDetailDto();
    user: UserEditDto = new UserEditDto();

    passwordComplexityInfo = '';
    profilePicture: string;
    fieldDate:Date;
    quantity: number;
    originalQuantity: number;
    
    constructor(
        injector: Injector,
        private _userService: UserServiceProxy,
        private _taskForStandardPartsServiceProxy: TaskForStandardPartsServiceProxy,
        private _profileService: ProfileServiceProxy
    ) {
        super(injector);
    }
    /**
     * 需求投产
     * @param requirementsForPartsDetailDto 
     */
    linkAdd(requirementsForPartsDetailDto: RequirementsForPartsDetailDto): void {
        if (requirementsForPartsDetailDto) {
            this.fieldDate = new Date();
            this.requirementsForPartsDetailDto = requirementsForPartsDetailDto;
            this.taskForStandardPartsDto.standardPartsDetailId = requirementsForPartsDetailDto.standardPartsDetailId;
            this.taskForStandardPartsDto.serialNumber = requirementsForPartsDetailDto.serialNumber;
            this._taskForStandardPartsServiceProxy.getDefaultProductionQuantity(requirementsForPartsDetailDto.standardPartsDetailId)
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.quantity = result;
                this.originalQuantity = result;
            });
        } 
        this.active = true;
        this.modal.show();
    }
    linkEdit(_ptaskForStandardPartsDto: TaskForStandardPartsDto): void {
        // 编辑时不知道originalQuantity，目前没有编辑
        if (_ptaskForStandardPartsDto) {
            this.fieldDate = _ptaskForStandardPartsDto.requiredTime.toDate();
            this.requirementsForPartsDetailDto.features = new StandardPartsFeatureDto();
            this.taskForStandardPartsDto = _ptaskForStandardPartsDto
        } 
        this.active = true;
        this.modal.show();
    }

    onShown(): void {
        
    }
    /**
     * 需求投产和编辑投产
     */
    save(): void {
        if (this.quantity == 0) {
            this.message.error("投产数量不能等于" + this.originalQuantity);
            return;
        }
        //按照业务部门要求，不限制投产数量必须小于需求总数，因此移除 2020-05-03 by songshn
        // if (this.originalQuantity < this.quantity) {
        //     this.message.error("投产数量不能大于" + this.originalQuantity);
        //     return;
        // }
        this.saving = true;
        this.taskForStandardPartsDto.requiredTime = moment(this.fieldDate)
        this.taskForStandardPartsDto.quantity = this.quantity;
        this._taskForStandardPartsServiceProxy.createOrUpdateTask(this.taskForStandardPartsDto)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }
    /**
     * 关闭窗口
     */
    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
